﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HDS.Common.Helper
{
    public static class DateTimeHelper
    {
        private static readonly DateTime UNIXBTIME = new DateTime(1970, 1, 1);

        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <param name="localTime">时间</param>
        /// <returns>int值</returns>
        public static int GetUnixTimeStamp(this DateTime localTime)
        {
            if (localTime <= UNIXBTIME)
                return 0;

            var of = new DateTimeOffset(localTime);
            return (int)of.ToUnixTimeSeconds();
        }

        /// <summary>
        /// 获取晚数
        /// </summary>
        /// <param name="CheckinDate"></param>
        /// <param name="CheckoutDate"></param>
        /// <returns></returns>
        public static int GetNights(DateTime CheckinDate, DateTime CheckoutDate)
        {
            TimeSpan ts = CheckoutDate - CheckinDate;
            return ts.Days;
        }

        /// <summary>
        /// 获取免费取消小时数，CancelDate < CheckinDate
        /// </summary>
        /// <param name="CheckInDate"></param>
        /// <param name="CancelDate"></param>
        /// <returns></returns>
        public static int GetFreeCancellationHours(DateTime CheckinDate, DateTime CancelDate)
        {
            TimeSpan ts = CheckinDate.AddDays(1) - CancelDate;
            var before = (int)ts.TotalHours;

            return before;
        }

        /// <summary>
        /// 获取免费取消时间点
        /// </summary>
        /// <param name="CheckinDate"></param>
        /// <param name="FreeCancellationTimeLimit"></param>
        /// <returns></returns>
        public static DateTime GetFreeCancellationTime(DateTime CheckinDate, int FreeCancellationTimeLimit)
        {
            return CheckinDate.AddDays(1).AddHours(-FreeCancellationTimeLimit);
        }
    }
}
